﻿using System;
using System.Text;
using NUnit.Framework;
using Selenium;

namespace time
{
    [TestFixture]
    internal class baidu1
    {
        Selenium.ISelenium selenium;

        [Test]
        public void t1()
        {
            selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.baidu.com");
            selenium.Start();
            selenium.Open("/");
            selenium.WindowMaximize();

            //selenium.WaitForPageToLoad("30000");
            System.Threading.Thread.Sleep(2000);

            //var v=selenium.GetValue("//a");
            //string script = s1();
            //string r=selenium.GetEval(script);
            //Console.WriteLine(r);

            string[] a = geta();
            foreach (var item in a)
            {
                try
                {
                    selenium.Open(item);
                    System.Threading.Thread.Sleep(5000);
                    selenium.GoBack();
                }
                catch (Exception ee)
                {
                    common.LogHelp.writelog(ee.ToString());
                    continue;
                }
            }

            Console.WriteLine("hello");
        }

        public static string s1()
        {
            string s = "var a='1';";
            //s += "a.toString();";
            return s;
        }

        public string[] geta()
        {
            StringBuilder strTrans = new StringBuilder();
            strTrans.Append("var inputId = new Array();\n");
            strTrans.Append("    var a = window.document.getElementsByTagName('a');\n");
            //document.write(a.toString());
            strTrans.Append("    for (var i = 0; i < a.length; i++) {\n");
            //document.write(a[i]);
            strTrans.Append("        if (a[i] != null && a[i] != 'undefined') {\n");
            strTrans.Append("            inputId[i] = a[i];\n");
            strTrans.Append("        }\n");
            strTrans.Append("    }\n");
            //document.write(inputId.toString());
            strTrans.Append("    inputId.toString()\n");
            strTrans.Append("\n");
            string script = strTrans.ToString();
            String[] checkboxIds = selenium.GetEval(script).Split(','); // Split the string.
            return checkboxIds;
        }
    }
}